package me.helllp.bootman.common.services;

import java.util.Locale;

import javax.annotation.Resource;

import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;

/**
 * 获取本地信息的服务
 * 
 * @author 虎头
 *
 */
public class LocaleMessageSourceService {
	@Resource
	private MessageSource messageSource;
	
	/**
	 * 获取对应key的信息
	 * 
	 * @param code	对应messages配置的key.
	 * @return
	 */
	public String getMessage(String code) {
		return this.getMessage(code, null, "", LocaleContextHolder.getLocale());
	}

	public String getMessage(String code, String... args) {
		Object[] arrObj = new Object[args.length];
        for (int i = 0; i < args.length; i++) {  
        	arrObj[i] = args[i];  
        }  
		return this.getMessage(code, arrObj);
	}

	public String getMessage(String code, Locale locale) {
		return messageSource.getMessage(code, null, "", locale);
	}

	public String getMessage(String code, Locale locale, String...args ) {
		return this.getMessage(code, args, "", locale);
	}
	
	public String getMessage(String code, Object[] args, Locale locale) {
		return this.getMessage(code, args, "", locale);
	}

	public String getMessage(String code, Object[] args) {
		// 这里使用比较方便的方法，不依赖request.
		return this.getMessage(code, args, "", LocaleContextHolder.getLocale());
	}

	public String getMessage(String code, Object[] args, String defaultMessage, Locale locale) {
		return messageSource.getMessage(code, args, defaultMessage, locale);
	}
}
